<template>
	<div id="allorder" >
        <div :style="opaction==true?'overflow:hidden;height:-webkit-fill-available;':''">
            <nav>
                <div :class="index == 0 ? 'active' : ''" @click="jumpto(0)">全部</div>
                <div :class="index == 1 ? 'active' : ''" @click="jumpto(1)">待付款</div>
                <div :class="index == 2 ? 'active' : ''" @click="jumpto(2)">待发货</div>
                <div :class="index == 3 ? 'active' : ''" @click="jumpto(3)">待收货</div>
                <div :class="index == 4 ? 'active' : ''" @click="jumpto(4)">待评价</div>
                <p :style="move"></p>
            </nav>
            <div class="wrap-swiper">
                <div class="swiper-container" style="overflow: visible;">
                    <div class="swiper-wrapper" :style="opaction==true?'-webkit-transform:none':''">
                        <div class="swiper-slide" style="clear:both;">
                            <v-all :height="height" :show="isShow.show" :list="list"> </v-all>
                        </div>
                        <div class="swiper-slide" >
                            <v-all :height="height" :show="isShow.show" :list="list"></v-all>
                        </div>
                        <div class="swiper-slide">
                            <v-all :height="height" :show="isShow.show" :list="list"></v-all>
                        </div>
                        <div class="swiper-slide">
                            <v-all :height="height" :show="isShow.show" :list="list"></v-all>
                        </div>
                        <div class="swiper-slide">
                            <v-all :height="height" :show="isShow.show" :list="list"></v-all>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div v-if="opaction" :style="styleHeight"></div>
        <div class="opaText" v-if="opaText">
            <div class="allP" v-html="$store.state.getContentText"></div>
            <ul class="opacul">
                <li class="opback" @click="hidePop">取消</li>
                <li class="opsure" @click="makesure($store.state.getOrderid,$store.state.getBytype)">确定</li>
            </ul>
        </div>
        <div class="context" v-if="shsh">
            <p>{{$store.state.getTextNobtn}}</p>
        </div>
	</div>
</template>

<script>
	import All from "./all.vue";
    import config from "../config/config.js";
    import share from "../share/share.js";
    import store from "../store/newStore.js";
    import MtaH5 from 'mta-h5-analysis';
	export default{
		components:{
			"v-all":All
		},
		data(){
			return{
				index:Number,
				move:{},
				swiper:{},
                height:'',
				isShow:{},
				list:[],
                orderId:'',
                btnType:'',
                contentText:'',
                shsh:false,
                opaction:false,
                winH:'',
                opaText:false,
                styleHeight:{'width': '100%','background': 'rgba(0, 0, 0, 0.5)','z-index': '1000','position': 'absolute','top': '0','height':'100%'}
			}
		},

		mounted(){
            this.$nextTick(function () {
                var hhh=document.getElementsByTagName('nav')[0].offsetHeight;
                var windowH=window.screen.height;
                this.height=windowH-hhh;
            })
		},
		methods:{
            hidePop(){
                this.opaction = false;
                this.shsh=false;
                this.opaText=false;
            },
            showPop(){
                this.opaction=true;
                this.shsh=true;
            },
            showToast(){
                this.opaction=true;
                this.opaText=true;
            },
			jumpto(index){
				this.index = index;
				var mySwiper = this.swiper;
				mySwiper.slideTo(index);
			},
            makesure(orderId,bt_type){
                if(bt_type=='CANCEL_ORDER'){
                    this.cancelOrder(orderId,bt_type)
                }else if(bt_type=='SURE_ORDER'){
                    this.sureOrder(orderId,bt_type)
                }else if(bt_type=='DELETE_ORDER'){
                    this.delectOrder(orderId,bt_type)
                }
            },
            checkOrder(type){
                var value=JSON.stringify({"page":"1","type":type,"token":localStorage.getItem('token')}),
                    key=demo(value);
                axios.post('/Home/Order/GetOrderInfo',{
                        value:value,
                        key:key
                },config)
                    .then(function(response) {
						if(response.data.data.data==''){
                             this.$set(this.isShow,'show',true);
                             this.swiperInit();
						}else{
						     this.list=response.data.data.data;
                             this.$set(this.isShow,'show',false);
                             this.swiperInit();
						}
                        this.$nextTick(function(){
                            var bodyH=document.documentElement.offsetHeight;
                            var allorderH=document.getElementById('allorder').offsetHeight;
                            if(allorderH>bodyH){
                                this.winH=allorderH+'px'
                            }else{
                                this.winH=document.documentElement.offsetHeight + 'px'
                            }
                        });
                    }.bind(this))
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            swiperInit(){
                var that = this;
                    this.swiper = new Swiper('.swiper-container', {
                        speed:300,
                        initialSlide: sessionStorage.getItem("index"),
                        calculateHeight:true,
                        observer: true,
                        observeParents: true,
                        onTransitionEnd: function(swiper){
                            that.index = swiper.activeIndex;
                        }
                    })
            },
            cancelOrder(orderid,bt_type){
                var value=JSON.stringify({"orderid":orderid,"bt_type":bt_type,"token":localStorage.getItem('token')}),
                    key=demo(value);
                axios.post('/Home/order/OrderButtonListener',{
                    value:value,
                    key:key
                },config)
                    .then(function(response) {
                        if(response.data.code==200){
                            var type=sessionStorage.getItem("index");
                            var that=this;
                            switch(type){
                                case '0':
                                    type='A';
                                    break;
                                case '1':
                                    type='C';
                                    break;
                                case '2':
                                    type='D';
                                    break;
                                case '3':
                                    type='E';
                                    break;
                                case '4':
                                    type='F';
                                    break;
                            }
                            this.opaText=false;
                            this.showPop();
                            setTimeout(function(){
                                that.hidePop();
                                that.checkOrder(type)
                            },1000)
                        }
                    }.bind(this))
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            sureOrder(orderid,bt_type){
                var value=JSON.stringify({"orderid":orderid,"bt_type":bt_type,"token":localStorage.getItem('token')}),
                    key=demo(value);
                axios.post('/Home/order/OrderButtonListener',{
                    value:value,
                    key:key
                },config)
                    .then(function(response) {
                        if(response.data.code==200){
                            var type=sessionStorage.getItem("index");
                            var that=this;
                            switch(type){
                                case '0':
                                    type='A';
                                    break;
                                case '1':
                                    type='C';
                                    break;
                                case '2':
                                    type='D';
                                    break;
                                case '3':
                                    type='E';
                                    break;
                                case '4':
                                    type='F';
                                    break;
                            }
                            this.opaText=false;
                            this.showPop();
                            setTimeout(function(){
                                that.hidePop();
                                that.checkOrder(type)
                            },1000)
                        }
                    }.bind(this))
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            delectOrder(orderid,bt_type){
                var value=JSON.stringify({"orderid":orderid,"bt_type":bt_type,"token":localStorage.getItem('token')}),
                    key=demo(value);
                axios.post('/Home/order/OrderButtonListener',{
                    value:value,
                    key:key
                },config)
                    .then(function(response) {
                        if(response.data.code==200){
                            var type=sessionStorage.getItem("index");
                            var that=this;
                            switch(type){
                                case '0':
                                    type='A';
                                    break;
                                case '1':
                                    type='C';
                                    break;
                                case '2':
                                    type='D';
                                    break;
                                case '3':
                                    type='E';
                                    break;
                                case '4':
                                    type='F';
                                    break;
                            }
                            this.opaText=false;
                            this.showPop();
                            setTimeout(function(){
                                that.hidePop();
                                that.checkOrder(type)
                            },1000)
                        }
                    }.bind(this))
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            payOrder(type,money,orderid){
                var that=this;
                var values=JSON.stringify({"type":type,"money":money,"orderid":orderid,"token":localStorage.getItem('token')}),
                    keys=demo(values);
                axios.post('/Home/Pay/OrderPay',{
                    value:values,
                    key:keys
                },config)
                    .then(function(res) {
                        if(res.data.code==200){
                            function jsApiCall()
                            {
                                WeixinJSBridge.invoke('getBrandWCPayRequest', {
                                        "appId": res.data.data.appId,     //公众号名称，由商户传入
                                        "timeStamp": res.data.data.timeStamp,//时间戳，自1970年以来的秒数
                                        "nonceStr": res.data.data.nonceStr, //随机串
                                        "package": res.data.data.package,
                                        "signType":"MD5",         //微信签名方
                                        "paySign": res.data.data.sign //微信签名
                                    },
                                    function(res){
                                        WeixinJSBridge.log(res.err_msg);
                                        if(res.err_msg == "get_brand_wcpay_request:ok"){
                                          // this.$router.push('/success/'+orderid);
                                          this.$router.push("/home/my");
                                        }else{
                                            window.location.href=location.href;
                                        }
                                    }
                                );
                            }
                            function callpay()
                            {
                                if (typeof WeixinJSBridge == "undefined"){
                                    if( document.addEventListener ){
                                        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                                    }else if (document.attachEvent){
                                        document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                                        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                                    }
                                }else{
                                    jsApiCall();
                                }
                            }
                            callpay();
                        }else if(res.data.code==216){
                            this.$store.commit('orderStatus',{"contentText":'',"textNobtn":res.data.msg,'orderid':orderid,"type":''});
                            var type=sessionStorage.getItem("index");
                            var that=this;
                            switch(type){
                                case '0':
                                    type='A';
                                    break;
                                case '1':
                                    type='C';
                                    break;
                                case '2':
                                    type='D';
                                    break;
                                case '3':
                                    type='E';
                                    break;
                                case '4':
                                    type='F';
                                    break;
                            }
                            this.opaText=false;
                            this.showPop();
                            setTimeout(function(){
                                that.hidePop();
                                that.checkOrder(type)
                            },1000)
                        }else if(res.data.code==215){
                            this.$store.commit('orderStatus',{"contentText":'',"textNobtn":res.data.msg,'orderid':orderid,"type":''});
                            var type=sessionStorage.getItem("index");
                            var that=this;
                            switch(type){
                                case '0':
                                    type='A';
                                    break;
                                case '1':
                                    type='C';
                                    break;
                                case '2':
                                    type='D';
                                    break;
                                case '3':
                                    type='E';
                                    break;
                                case '4':
                                    type='F';
                                    break;
                            }
                            this.opaText=false;
                            this.showPop();
                            setTimeout(function(){
                                that.hidePop();
                                that.checkOrder(type)
                            },1000)
                        }


                    }.bind(this))
                    .catch(function (error) {
                        console.log(error);
                    });
            },
		},
		watch:{
			"index":function(){
				this.move = {
					left: document.body.offsetWidth / 5 * this.index + "px"
				}
				sessionStorage.setItem("index",this.index);
                 var type=sessionStorage.getItem("index");
                   switch(type){
                       case '0':
                           type='A';
                           break;
                       case '1':
                           type='C';
                           break;
                       case '2':
                           type='D';
                           break;
                       case '3':
                           type='E';
                           break;
                       case '4':
                           type='F';
                           break;
                   }
                   this.checkOrder(type)
			}
		},
		activated(){
            var _self=this;
            var type=sessionStorage.getItem("index");
            this.index=sessionStorage.getItem('index');
            if(this.$route.query.showWrapper){
                this.opaction=true;
                this.shsh=true;
                this.$store.commit('orderStatus',{"contentText":'',"textNobtn":"发表追评成功",'orderid':'',"type":''});
                setTimeout(()=>{
                    _self.opaction=false;
                    _self.shsh=false;
                },2000)
            }
            switch(type){
                case '0':
                    type='A';
                    break;
                case '1':
                    type='C';
                    break;
                case '2':
                    type='D';
                    break;
                case '3':
                    type='E';
                    break;
                case '4':
                    type='F';
                    break;
            }
            this.checkOrder(type);
            share({});
            MtaH5.init({
                "sid":'500594815',
                "cid":'500595819',
                "autoReport":0,
                "senseHash":0,
                "senseQuery":0,
                "performanceMonitor":0,
                "ignoreParams":[]
            });
            MtaH5.pgv();
		}
	}
</script>

<style>

</style>